#include<bits/stdc++.h>
using namespace std;
int t,n,m,pw[100005],a[100005],b[100005],c[100005],wn[18][100005],g[4];
bool d[18][100005];
char s[100005];
bool vis[100005];
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=1;i<=m;i++)scanf("%d",&c[i]);
    pw[1]=0;
    for(int i=2;i<=n;i++)pw[i]=pw[i+1>>1]+1;
    for(int i=1;i<=pw[n];i++){
        scanf("%s",s+1);
        for(int j=1;s[j];j++)d[i][j]=(s[j]^48);
    }
    scanf("%d",&t);
    while(t--){
        for(int i=0;i<4;i++)scanf("%d",&g[i]);
        for(int i=1;i<=n;i++)a[i]=b[i]^g[i%4];
        int ans=0;
        if(n<=8){
            for(int i=1;i<=m;i++){
                int x=1<<pw[c[i]],res=0;
                for(int j=1;j<=8;j++)vis[j]=0;
                for(int j=0;j<(1<<(x-c[i])*2);j++){
                    for(int k=1;k<=x;k++)wn[0][k]=k;
                    for(int k=c[i]+1;k<=x;k++)a[k]=j>>(k-c[i]-1)*2&3;
                    for(int k=1;k<=pw[c[i]];k++)
                        for(int l=1;l<=(1<<pw[c[i]]-k);l++){
                            if(d[k][l])swap(wn[k-1][l*2],wn[k-1][l*2-1]);
                            wn[k][l]=wn[k-1][l*2-(a[wn[k-1][l*2-1]]>=k)];
                        }
                    int wnr=wn[pw[c[i]]][1];
                    if(!vis[wnr])res+=wnr,vis[wnr]=1;
                }
                ans^=res*i;
            }
        }
        else{
            for(int i=1;i<=m;i++){
                int res=0;
                for(int j=1;j<=c[i];j++)wn[0][j]=j,vis[j]=0;
                for(int j=1;(1<<j)<=c[i];j++)for(int k=1;k<=(1<<pw[c[i]]-j);k++){
                    if(d[j][k])swap(wn[j-1][k*2],wn[j-1][k*2-1]);
                    wn[j][k]=wn[j-1][k*2-(a[wn[j-1][k*2-1]]>=j)];
                }
                int wnr=wn[pw[c[i]]][1];
                if(!vis[wnr])res+=wnr,vis[wnr]=1;
                ans^=res*i;
            }
        }
        printf("%d\n",ans);
    }
}
